package com.peakmain.homemodule.presenter;

import android.annotation.SuppressLint;

import com.peakmain.homemodule.contarct.HomeArticleContract;
import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;
import com.peakmain.wanandroid.netmodule.bean.home.HomeArticleBean;
import com.peakmain.wanandroid.netmodule.bean.home.HomeBannerBean;
import com.peakmain.wanandroid.netmodule.bean.home.HomeFriendBean;
import com.peakmain.wanandroid.netmodule.bean.home.HomeTopArticleBean;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/17
 * mail: 2726449200@qq.com
 * desiption:
 */
public class HomeArticlePresenter extends BasePresenter<HomeArticleContract.View> implements HomeArticleContract.Presenter {
    @Inject
    public HomeArticlePresenter() {

    }

    @SuppressLint("CheckResult")
    @Override
    public void getHomeArticle(int page, final boolean isPullDown, final boolean isSilence) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getHomeArticle(page)
                .compose(mView.<DataResponse<HomeArticleBean>>bindToLife())
                .compose(RxSchedulers.<DataResponse<HomeArticleBean>>applySchedulers())
                .subscribe(new Consumer<DataResponse<HomeArticleBean>>() {
                    @Override
                    public void accept(DataResponse<HomeArticleBean> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showHomeArticleSuccess(dataResponse.getData(), isPullDown, isSilence);
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }

    @SuppressLint("CheckResult")
    @Override
    public void getHomeBanner() {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getHomeBanner()
                .compose(mView.<DataResponse<List<HomeBannerBean>>>bindToLife())
                .compose(RxSchedulers.<DataResponse<List<HomeBannerBean>>>applySchedulers())
                .subscribe(new Consumer<DataResponse<List<HomeBannerBean>>>() {
                    @Override
                    public void accept(DataResponse<List<HomeBannerBean>> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showHomeBannerSuccess(dataResponse.getData());
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }

    @SuppressLint("CheckResult")
    @Override
    public void getHomeTopArticle() {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getHomeTopArticle()
                .compose(mView.<DataResponse<List<HomeTopArticleBean>>>bindToLife())
                .compose(RxSchedulers.<DataResponse<List<HomeTopArticleBean>>>applySchedulers())
                .subscribe(new Consumer<DataResponse<List<HomeTopArticleBean>>>() {
                    @Override
                    public void accept(DataResponse<List<HomeTopArticleBean>> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0)
                            mView.showHomeTopArticle(dataResponse.getData());
                        else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }

                },new Consumer<Throwable>()

                {
                    @Override
                    public void accept (Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }

    @SuppressLint("CheckResult")
    @Override
    public void getFriendData() {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getFriendData()
                .compose(mView.<DataResponse<List<HomeFriendBean>>>bindToLife())
                .compose(RxSchedulers.<DataResponse<List<HomeFriendBean>>>applySchedulers())
                .subscribe(new Consumer<DataResponse<List<HomeFriendBean>>>() {
                    @Override
                    public void accept(DataResponse<List<HomeFriendBean>> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0)
                            mView.showFriendDataSuccess(dataResponse.getData());
                        else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }

                },new Consumer<Throwable>()

                {
                    @Override
                    public void accept (Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }
}
